package com.donkey.test;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;

/**
 * 金额格式化测试
 */
public class DoubleFormat {
    public static void main(String[] args) {
        String MSG_DYNAMIC_PSALARY_IN = "%s钱包到账%.2f元，去看看";
        System.out.println(String.format(MSG_DYNAMIC_PSALARY_IN, "Hello", 121.0)); //121.00
        System.out.println(String.format(MSG_DYNAMIC_PSALARY_IN, "Hello", 121.01));
        System.out.println(String.format(MSG_DYNAMIC_PSALARY_IN, "Hello", 121.014));
        System.out.println(String.format(MSG_DYNAMIC_PSALARY_IN, "Hello", 121.015)); //121.02
        System.out.println(String.format(MSG_DYNAMIC_PSALARY_IN, "Hello", 121.115)); //121.12
        System.out.println(String.format(MSG_DYNAMIC_PSALARY_IN, "Hello", 121.016));

        DecimalFormat format = new DecimalFormat("0.00"); //靠谱
        System.out.println(format.format(121.0));
        System.out.println(format.format(121.01));
        System.out.println(format.format(121.014));
        System.out.println(format.format(121.015)); //121.02
        System.out.println(format.format(121.115)); //121.11
        System.out.println(format.format(121.016));
        System.out.println(format.format(100000000000000001.0111));
        System.out.println("---------BigDecimal BigDecimal---------");
        DecimalFormat format1 = new DecimalFormat("0.##"); //靠谱
        BigDecimal decimal1 = new BigDecimal(1000000000000000010L).movePointLeft(2);
        System.out.println(decimal1.toPlainString());
        System.out.println(decimal1.toString());
        System.out.println(format1.format(decimal1));
        decimal1 = new BigDecimal(100).movePointLeft(2);
        System.out.println(decimal1.toPlainString());
        System.out.println(decimal1.toString());
        System.out.println(format1.format(decimal1));
        System.out.println("---------BigDecimal---------");
        BigDecimal decimal = new BigDecimal(100000000000000001.0111);
        System.out.println(format.format(decimal));
        decimal = new BigDecimal("100000000000000001.0111");
        System.out.println(format.format(decimal));
        System.out.println(format.format(new BigDecimal("121.1")));
        System.out.println(format.format(new BigDecimal("121.015"))); //121.02
        System.out.println(format.format(new BigDecimal("121.115"))); //121.12
        System.out.println("---------BigDecimal---------");
        BigDecimal mul = new BigDecimal("121.115").multiply(new BigDecimal("2.0"));
        System.out.println(format.format(mul)); //242.23
        DecimalFormat formatToInt = new DecimalFormat("0"); //靠谱
        mul = new BigDecimal("1234.4445");
        System.out.println(format.format(mul)); //1234.44
        System.out.println(formatToInt.format(mul)); //1234
        System.out.println(mul.intValue()); //1234
        System.out.println("---------1234.5445---------");
        mul = new BigDecimal("1234.5445");
        System.out.println(formatToInt.format(mul)); //1235
        System.out.println(mul.intValue()); //1234
        System.out.println(mul.setScale(0, BigDecimal.ROUND_HALF_UP).longValue()); //1234
        System.out.println(mul.movePointRight(2)); //123454.45
        System.out.println(mul.movePointRight(5)); //123454450

        System.out.println("---------Double---------");
        System.out.println(new Double(1.0).toString());
        System.out.println(new Double(1.01).toString());
        System.out.println(new Double(1.011).toString());
        System.out.println(new Double(100000000000000001.0111).toString());
        System.out.println(String.format("%f", new Double(100000000000000001.0111)));
        System.out.println(new Float(1.0).toString());
        System.out.println(new Float(1.01).toString());
        System.out.println(new Float(1.011).toString());
        System.out.println(new Float(10000000001.0111).toString());
        System.out.println(String.format("%f", new Float(10000000001.0111)));

//        DoubleFormat stringFormat = new DoubleFormat();
//        try {
//            stringFormat.throwEx();
//        } catch (Exception e) {
//            System.out.println(ExceptionUtils.getExceptionPosition());
//            e.printStackTrace();;
//        }
    }

    private void throwEx() {
        throwException();
    }

    private void throwException() {
        String string = null;
        System.out.println(string.toString());
    }

}
